package com.guiji.quartz.domain;

import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.wanghaomiao.xpath.model.JXDocument;
import com.guiji.quartz.config.ProxyConfig;
import com.guiji.quartz.task.DataSaveUtil;
import com.guiji.quartz.util.DateFormatUtil;
import com.guiji.quartz.util.DelPicUtil;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @date 2021/11/25 9:22
 */
public class Investors {
	private static final Logger log = LoggerFactory.getLogger(Investors.class);

	public static void mainMethod() throws Exception {
		List<String> list = new ArrayList<>();
		list.add("https://www.investors.com/technology/");
		list.add("https://www.investors.com/economic-news-president-job-approval/");
		list.add("https://www.investors.com/news/technology/fang-stocks-news-quotes-facebook-amazon-netflix-google/");
		list.forEach(url -> {
			HttpResponse response = null;
			try {
				response = HttpRequest.get(url)
					.header("Accept", "*/*")
					.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36")
					.header("Accept-Encoding", "gzip, deflate")
					.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
					.setConnectionTimeout(15000)
					.execute();
			} catch (Exception e) {
				log.error("代理失败1-Investors" + e);
				return;
			}
			String category = "Investors-BusinessTech";
			if (url.contains("job")) {
				category = "investors-Economy";
			}
			if (url.contains("stocks")) {
				category = "investors-Stock";
			}
			if (null != response && response.getStatus() == 200) {
				List<HttpCookie> cookies = response.getCookies();
				String resp = response.body();
				Set<String> resUrl = getUrl(resp);
				getContent(resUrl, cookies, category);
			} else {
				log.error("访问失败1-Investors");
			}
		});


	}

	private static void getContent(Set<String> listContent, List<HttpCookie> cookies, String category) {
		for (String url : listContent) {
			log.info("getContent执行到-{},listContent长度-{}", url, listContent.size());
			String doc = null;
			JXDocument jxDocument = null;
			try {
				if (null != cookies && cookies.size() > 0) {
					CookieManager ccm = new CookieManager();
					CookieStore CookieStore = ccm.getCookieStore();
					for (HttpCookie cookie : cookies) {
						URL domain = new URL("https://www.investors.com");
						CookieStore.add(domain.toURI(), cookie);
					}
					HttpRequest.setCookieManager(ccm);
				}

				HttpResponse response = HttpRequest.get(url)
					.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36")
					.header("Host", "www.investors.com")
					.header("Referer", "https://www.investors.com/news/technology/fang-stocks-news-quotes-facebook-amazon-netflix-google/")
					.setConnectionTimeout(15000).execute();
				doc = response.body();
				if (null == doc || doc.isBlank() || doc.isEmpty()) {
					log.info("请求详情页为空");
					continue;
				}
				jxDocument = new JXDocument(doc);
			} catch (Exception e) {
				log.info("请求详情页失败" + e);
				continue;
			}
			String title = null;
			try {
				title = (String) jxDocument.sel("//h1//text()").get(0);
			} catch (Exception e) {
				log.error("标题错误-------------" + url + "-------------------标题错误");
				continue;
			}
			Document document = null;
			String content = null;
			try {
				document = Jsoup.parse(doc);
				content = document.getElementsByClass("single-post-content post-content drop-cap").get(0).toString();
				content = content.replaceAll("</p>|</h2>", "\\$\\$\\$").replaceAll("\n", "")
					.replaceAll("<html.*?</html>", "").replaceAll("<span.*?</span>", "")
					.replaceAll("<script.*?</script>", "").replaceAll("<.*?>", "").replaceAll("IBD Newsletters.*?CLOSE", "");
				String[] sss = content.split("YOU MAY ALSO LIKE");
				content = sss[0];
			} catch (Exception e) {
				continue;
			}

			List<String> newImgs = new ArrayList<>();

			Date date = new Date();

			if (content.length() > 50000 || content.replaceAll("\\$|[\\s\\p{Zs}]", "").length() < 20 || newImgs.size() > 20 || content.contains("Ratings$$$")) {
				continue;
			}
			try {
				DataSaveUtil.saveData(title, content, newImgs, url, date, "https://www.investors.com/technology/||", category);
				log.info(Thread.currentThread().getName() + "-----------------{}----------------", category);
			} catch (Exception e) {
				continue;
			}
		}
	}

	private static Set<String> getUrl(String content) {

		Set<String> res = new HashSet<>();
		String pattern = "https://www.investors.com/news/(technology)?(economy)?/.*?/";
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(content);
		while (m.find()) {
			res.add(m.group());
		}
		return res;
	}

//	public static void main(String[] args) throws Exception {
//		mainMethod();
//	}

}

